home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / numeral.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  15.3 KB  |  542 lines

  1. #ifndef __NUMERAL_CC
  2. #define __NUMERAL_CC
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * numeral.cc - Definitions for the Standard Library numeric facets
  8.  *
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  **************************************************************************/
  43.  
  44. #ifndef __STD_RW_LOCNUMRW__
  45. #include <rw/numbrw>
  46. #endif
  47.  
  48. #ifndef _RWSTD_NO_NAMESPACE
  49. namespace __rwstd {
  50. #endif
  51.  
  52. // -----------------------------------------------
  53. // Template numpunct_data<charT> member functions.
  54. // -----------------------------------------------
  55.  
  56. template <class charT>
  57. void numpunct_data<charT>::rw_init (void) {
  58.   tf_defs_[0].s=tn_.c_str();
  59.   tf_defs_[0].v=1;
  60.   tf_defs_[1].s=fn_.c_str();
  61.   tf_defs_[1].v=0;
  62.  
  63.   tf_map_.num_defs_=2 ;
  64.   tf_map_.defs_=tf_defs_;
  65. }
  66.  
  67. template <class charT>
  68. numpunct_init<charT>*
  69. _RWSTDExport fixup_numpunct_init
  70.     (numpunct_init<char> *init,charT*)
  71. {
  72.   if (init->del_)
  73.     delete[] (char*) init;
  74.   return NULL;
  75. }
  76.  
  77. template <class charT>
  78. numpunct_init<charT>*
  79. numpunct_data<charT>::get_init_by_name_
  80.     (const char *name)
  81. {
  82.   return fixup_numpunct_init(get_named_init_(name),(charT*)0);
  83. }
  84.  
  85.  
  86. #ifndef _RWSTD_NO_NAMESPACE
  87. } namespace std {
  88. #endif
  89.  
  90.  
  91.  
  92. // ----------------------------------------------------
  93. // Facet num_get<charT,InputIterator> member templates.
  94. // ----------------------------------------------------
  95.  
  96.  
  97. template <class charT, class InputIterator>
  98. locale::id num_get<charT, InputIterator>::id;
  99.  
  100. template <class charT, class InputIterator>
  101. num_get<charT,InputIterator>::~num_get() { }
  102.  
  103. #ifndef _RWSTD_NO_BOOL
  104.  
  105. template <class charT, class InputIterator>
  106. InputIterator num_get<charT,InputIterator>::do_get
  107.     (InputIterator in, InputIterator end, ios_base& io,
  108.      ios_base::iostate& err, bool& value) const
  109. {
  110.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  111.   long v=reader.to_ulong(reader.get_int_digits());
  112.   err=ios_base::goodbit; // aka 0
  113.  
  114.   if (!reader.error)
  115.     if (v==0)
  116.       value=false;
  117.     else if (v==1 && !reader.negative)
  118.       value=true;
  119.     else
  120.       err=ios_base::failbit;
  121.   else if (reader.advanced)
  122.     err=ios_base::failbit;
  123.   else {
  124.     const numpunct<charT>& nump =
  125.         _RWSTD_STATIC_CAST(const numpunct<charT>&,reader.punct);
  126.     int k=reader.get_keyword(reader.get_tf_map(nump));
  127.     if (k<0)
  128.       err=ios_base::failbit;
  129.     else
  130.       value=k;
  131.   }
  132.  
  133.   if (reader.reached_end)
  134.     err|=ios_base::eofbit;
  135.  
  136.   return in;
  137. }
  138.  
  139. #endif // _RWSTD_NO_BOOL
  140.  
  141. template <class charT, class InputIterator>
  142. InputIterator num_get<charT,InputIterator>::do_get
  143.     (InputIterator in, InputIterator end, ios_base& io,
  144.      ios_base::iostate& err, void*& value) const
  145. {
  146.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  147.   void *v=reader.to_pointer(reader.get_pointer_digits());
  148.   err=ios_base::goodbit;
  149.  
  150.   if (reader.error)
  151.     err=ios_base::failbit;
  152.   else
  153.     value=v;
  154.  
  155.   if (reader.reached_end)
  156.     err|=ios_base::eofbit;
  157.  
  158.   return in;
  159. }
  160.  
  161. template <class charT, class InputIterator>
  162. InputIterator num_get<charT,InputIterator>::do_get
  163.     (InputIterator in, InputIterator end, ios_base& io,
  164.      ios_base::iostate& err, long& value) const
  165. {
  166.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  167.   long v=reader.to_ulong(reader.get_int_digits());
  168.   err=ios_base::goodbit;
  169.  
  170.   if (!reader.error)
  171.     if (reader.negative) {
  172.       if ((v=-v)>0)
  173.         reader.error=reader.overflow;
  174.     } else
  175.       if (v<0 && reader.radix==10)
  176.         reader.error=reader.overflow;
  177.  
  178.   if (reader.error)
  179.     err=ios_base::failbit;
  180.   else
  181.     value=v;
  182.  
  183.   if (reader.reached_end)
  184.     err|=ios_base::eofbit;
  185.     
  186.   return in;
  187. }
  188.  
  189. #ifdef _RWSTD_LONG_LONG
  190.  
  191. template <class charT, class InputIterator>
  192. InputIterator num_get<charT,InputIterator>::do_get
  193.     (InputIterator in, InputIterator end, ios_base& io,
  194.      ios_base::iostate& err, _RWSTD_LONG_LONG& value) const
  195. {
  196.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  197.   _RWSTD_LONG_LONG v=reader.to_ulong_ulong(reader.get_int_digits());
  198.   err=ios_base::goodbit;
  199.  
  200.   if (!reader.error)
  201.     if (reader.negative) {
  202.       if ((v=-v)>0)
  203.         reader.error=reader.overflow;
  204.     } else
  205.       if (v<0 && reader.radix==10)
  206.         reader.error=reader.overflow;
  207.  
  208.   if (reader.error)
  209.     err=ios_base::failbit;
  210.   else
  211.     value=v;
  212.  
  213.   if (reader.reached_end)
  214.     err|=ios_base::eofbit;
  215.     
  216.   return in;
  217. }
  218.  
  219. #endif // _RWSTD_LONG_LONG
  220.  
  221. template <class charT, class InputIterator>
  222. InputIterator num_get<charT,InputIterator>::do_get
  223.     (InputIterator in, InputIterator end, ios_base& io,
  224.      ios_base::iostate& err, unsigned short& value) const
  225. {
  226.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  227.   unsigned long v=reader.to_ulong(reader.get_int_digits());
  228.   err=ios_base::goodbit;
  229.  
  230.   if (reader.error
  231.       || v > (unsigned long) numeric_limits<unsigned short>::max()
  232.       || (reader.negative && v!=0))
  233.     err=ios_base::failbit;
  234.   else
  235.     value=v;
  236.  
  237.   if (reader.reached_end)
  238.     err|=ios_base::eofbit;
  239.  
  240.   return in;
  241. }
  242.  
  243. template <class charT, class InputIterator>
  244. InputIterator num_get<charT,InputIterator>::do_get
  245.     (InputIterator in, InputIterator end, ios_base& io,
  246.      ios_base::iostate& err, unsigned int& value) const
  247. {
  248.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  249.   unsigned long v=reader.to_ulong(reader.get_int_digits());
  250.   err=ios_base::goodbit;
  251.  
  252.   if (reader.error
  253.       || v > (unsigned long) numeric_limits<unsigned int>::max()
  254.       || (reader.negative && v!=0))
  255.     err=ios_base::failbit;
  256.   else
  257.     value=v;
  258.  
  259.   if (reader.reached_end)
  260.     err|=ios_base::eofbit;
  261.  
  262.   return in;
  263. }
  264.  
  265. template <class charT, class InputIterator>
  266. InputIterator num_get<charT,InputIterator>::do_get
  267.     (InputIterator in, InputIterator end, ios_base& io,
  268.      ios_base::iostate& err, unsigned long& value) const
  269. {
  270.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  271.   unsigned long v=reader.to_ulong(reader.get_int_digits());
  272.   err=ios_base::goodbit;
  273.  
  274.   if (reader.error || (reader.negative && v!=0))
  275.     err=ios_base::failbit;
  276.   else
  277.     value=v;
  278.  
  279.   if (reader.reached_end)
  280.     err|=ios_base::eofbit;
  281.  
  282.   return in;
  283. }
  284.  
  285. template <class charT, class InputIterator>
  286. InputIterator num_get<charT,InputIterator>::do_get
  287.     (InputIterator in, InputIterator end, ios_base& io,
  288.      ios_base::iostate& err, float& value) const
  289. {
  290.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  291.   float v=reader.to_float(reader.get_float_digits());
  292.   err=ios_base::goodbit;
  293.  
  294.   if (reader.error)
  295.     err=ios_base::failbit;
  296.   else
  297.     value=v;
  298.  
  299.   if (reader.reached_end)
  300.     err|=ios_base::eofbit;
  301.  
  302.   return in;
  303. }
  304.  
  305. template <class charT, class InputIterator>
  306. InputIterator num_get<charT,InputIterator>::do_get
  307.     (InputIterator in, InputIterator end, ios_base& io,
  308.      ios_base::iostate& err, double& value) const
  309. {
  310.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  311.   double v=reader.to_double(reader.get_float_digits());
  312.   err=ios_base::goodbit;
  313.  
  314.   if (reader.error)
  315.     err=ios_base::failbit;
  316.   else
  317.     value=v;
  318.  
  319.   if (reader.reached_end)
  320.     err|=ios_base::eofbit;
  321.  
  322.   return in;
  323. }
  324.  
  325. template <class charT, class InputIterator>
  326. InputIterator num_get<charT,InputIterator>::do_get
  327.     (InputIterator in, InputIterator end, ios_base& io,
  328.      ios_base::iostate& err, long double& value) const
  329. {
  330.   __RWSTD::digit_reader<charT,InputIterator> reader(in,end,io);
  331.   long double v=reader.to_long_double(reader.get_float_digits());
  332.   err=ios_base::goodbit;
  333.  
  334.   if (reader.error)
  335.     err=ios_base::failbit;
  336.   else
  337.     value=v;
  338.  
  339.   if (reader.reached_end)
  340.     err|=ios_base::eofbit;
  341.  
  342.   return in;
  343. }
  344.  
  345. // -----------------------------------------------------
  346. // Facet num_put<charT,OutputIterator> member templates.
  347. // -----------------------------------------------------
  348.  
  349. template <class charT, class OutputIterator>
  350. locale::id num_put<charT, OutputIterator>::id;
  351.  
  352. template <class charT, class OutputIterator>
  353. num_put<charT,OutputIterator>::~num_put() { }
  354.  
  355. #ifndef _RWSTD_NO_BOOL
  356.  
  357. template <class charT, class OutputIterator>
  358. OutputIterator num_put<charT,OutputIterator>::do_put
  359.     (OutputIterator out, ios_base& io, charT fill, bool value) const
  360. {
  361.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  362.   if (io.flags() & ios_base::boolalpha) {
  363.     const numpunct<charT>& nump =
  364.         _RWSTD_STATIC_CAST(const numpunct<charT>&,writer.punct);
  365.     writer.put_keyword(writer.get_tf_string(nump,value),fill);
  366.   } else {
  367.     writer.digitize((unsigned long) (value? 1 : 0));
  368.     writer.put_digits(fill);
  369.   }
  370.   return out;
  371. }
  372.  
  373. #endif // _RWSTD_NO_BOOL
  374.  
  375. template <class charT, class OutputIterator>
  376. OutputIterator num_put<charT,OutputIterator>::do_put
  377.     (OutputIterator out, ios_base& io, charT fill, void* value) const
  378. {
  379.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  380.   writer.digitize(value);
  381.   writer.put_digits(fill);
  382.   return out;
  383. }
  384.  
  385. template <class charT, class OutputIterator>
  386. OutputIterator num_put<charT,OutputIterator>::do_put
  387.     (OutputIterator out, ios_base& io, charT fill, long value) const
  388. {
  389.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  390.   writer.digitize(value);
  391.   writer.put_digits(fill);
  392.   return out;
  393. }
  394.  
  395. template <class charT, class OutputIterator>
  396. OutputIterator num_put<charT,OutputIterator>::do_put
  397.     (OutputIterator out, ios_base& io, charT fill, unsigned long value) const
  398. {
  399.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  400.   writer.digitize(value);
  401.   writer.put_digits(fill);
  402.   return out;
  403. }
  404.  
  405. #ifdef _RWSTD_LONG_LONG
  406.  
  407. template <class charT, class OutputIterator>
  408. OutputIterator num_put<charT,OutputIterator>::do_put
  409.     (OutputIterator out, ios_base& io, charT fill, _RWSTD_LONG_LONG val) const
  410. {
  411.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  412.   writer.digitize(val);
  413.   writer.put_digits(fill);
  414.   return out;
  415. }
  416.  
  417. #endif // _RWSTD_LONG_LONG
  418.  
  419. template <class charT, class OutputIterator>
  420. OutputIterator num_put<charT,OutputIterator>::do_put
  421.     (OutputIterator out, ios_base& io, charT fill, double value) const
  422. {
  423.   __RWSTD::digit_writer<charT,OutputIterator> digits(out,io);
  424.   digits.digitize(value);
  425.   digits.put_digits(fill);
  426.   return out;
  427. }
  428.  
  429. #ifndef _RWSTD_NO_LONG_DOUBLE
  430. template <class charT, class OutputIterator>
  431. OutputIterator num_put<charT,OutputIterator>::do_put
  432.     (OutputIterator out, ios_base& io, charT fill, long double value) const
  433. {
  434.   __RWSTD::digit_writer<charT,OutputIterator> digits(out,io);
  435.   digits.digitize(value);
  436.   digits.put_digits(fill);
  437.   return out;
  438. }
  439. #endif // _RWSTD_NO_LONG_DOUBLE
  440.  
  441. template <class charT, class OutputIterator>
  442. OutputIterator num_put<charT,OutputIterator>::do_put
  443.     (OutputIterator out, ios_base& io, charT fill, short value) const
  444. {
  445.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  446.   writer.digitize(value);
  447.   writer.put_digits(fill);
  448.   return out;
  449. }
  450.  
  451. template <class charT, class OutputIterator>
  452. OutputIterator num_put<charT,OutputIterator>::do_put
  453.     (OutputIterator out, ios_base& io, charT fill, unsigned short value) const
  454. {
  455.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  456.   writer.digitize(value);
  457.   writer.put_digits(fill);
  458.   return out;
  459. }
  460.  
  461. template <class charT, class OutputIterator>
  462. OutputIterator num_put<charT,OutputIterator>::do_put
  463.     (OutputIterator out, ios_base& io, charT fill, int value) const
  464. {
  465.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  466.   writer.digitize(value);
  467.   writer.put_digits(fill);
  468.   return out;
  469. }
  470.  
  471. template <class charT, class OutputIterator>
  472. OutputIterator num_put<charT,OutputIterator>::do_put
  473.     (OutputIterator out, ios_base& io, charT fill, unsigned int value) const
  474. {
  475.   __RWSTD::digit_writer<charT,OutputIterator> writer(out,io);
  476.   writer.digitize(value);
  477.   writer.put_digits(fill);
  478.   return out;
  479. }
  480.  
  481. // ---------------------------------------
  482. // Facet numpunct<charT> member templates.
  483. // ---------------------------------------
  484.  
  485. template <class charT>
  486. locale::id numpunct<charT>::id;
  487.  
  488. template <class charT>
  489. numpunct<charT>::~numpunct() { }
  490.  
  491. template <class charT>
  492. charT numpunct<charT>::do_decimal_point () const { return this->dp_; }
  493.  
  494. template <class charT>
  495. charT numpunct<charT>::do_thousands_sep () const { return this->ts_; }
  496.  
  497. template <class charT>
  498. string numpunct<charT>::do_grouping () const { return this->gr_; }
  499.  
  500. template <class charT>
  501. _TYPENAME numpunct<charT>::string_type
  502. numpunct<charT>::do_falsename () const { return this->fn_; }
  503.  
  504. template <class charT>
  505. _TYPENAME numpunct<charT>::string_type
  506. numpunct<charT>::do_truename () const { return this->tn_; }
  507.  
  508. template <class charT>
  509. void numpunct<charT>::rw_init () {
  510.   this->dp_=do_decimal_point();
  511.   this->ts_=do_thousands_sep();
  512.   this->gr_=do_grouping();
  513.   this->fn_=do_falsename();
  514.   this->tn_=do_truename();
  515.  
  516. #ifndef _RWSTD_NO_NAMESPACE
  517.   __rwstd::numpunct_impl<charT>::rw_init();
  518. #else
  519.   numpunct_impl<charT>::rw_init();
  520. #endif
  521. }
  522.  
  523. // ----------------------------------------------------------------------
  524. // Numeric punctuation by-name member templates: numpunct_byname<charT>
  525. // ----------------------------------------------------------------------
  526.  
  527. template <class charT>
  528. numpunct_byname<charT>::numpunct_byname (const char *n, size_t refs):
  529.     numpunct<charT>(refs,get_init_by_name_(n))
  530. { }
  531.  
  532. template <class charT>
  533. numpunct_byname<charT>::~numpunct_byname()
  534. { }
  535.  
  536. #ifndef _RWSTD_NO_NAMESPACE
  537. }
  538. #endif
  539.  
  540. #pragma option pop
  541. #endif /* __NUMERAL_CC */
  542.